package com.fjec.paid.controller;

import com.fjec.common.protocol.JsonResult;
import com.fjec.common.util.JsonUtil;
import com.fjec.common.ys.PaidHelper;
import com.fjec.paid.service.IBizOrderPaymentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/")
public class YsNotifyController {

    @Resource
    private PaidHelper paidHelper;

    @Resource
    private IBizOrderPaymentService bizOrderPaymentService;

    @PostMapping("/prepare/pay")
    public JsonResult preparePay(String openId) throws Exception {
        return JsonResult.SUCCESS;

    }

    /**
     * 银盛回调接口
     * @param map
     * @return
     */
    @PostMapping("/n/notify/pay")
    public String payNotify(@RequestBody Map<String, Object> map) {
        String notifyTime = MapUtils.getString(map, "notify_time");
        String outTradeNo = MapUtils.getString(map, "out_trade_no");
        String totalAmount = MapUtils.getString(map, "total_amount");
        String tradeNo = MapUtils.getString(map, "trade_no");
        String tradeStatus = MapUtils.getString(map, "trade_status");
        String accountDate = MapUtils.getString(map, "account_date");

        try {
            bizOrderPaymentService.payNotify(notifyTime, outTradeNo, totalAmount, tradeNo, tradeStatus, accountDate, 1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "success";
    }

    @PostMapping("/get/upload/token")
    public JsonResult getUploadToken() throws Exception  {
        String token = paidHelper.getUploadToken();
        return JsonResult.success()
                .data(token)
                .build();
    }

    @PostMapping("/upload")
    public JsonResult upload(String token, MultipartFile file) throws Exception {
        paidHelper.upload(token, "19", file.getBytes());
        return JsonResult.success()
                .build();
    }

    @GetMapping("/test")
    public String test() {
        return "ok";
    }
}
